<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <link rel="stylesheet" href="resource/weui.min.css"/>
	<link rel="stylesheet" href="resource/example.css"/>
   	<meta charset="utf-8">
	<meta name="keywords" content="" />
	<meta name="description" content="" />
	<meta content="width=device-width, minimum-scale=1,initial-scale=1, maximum-scale=1, user-scalable=1" id="viewport" name="viewport" />
	<meta content="yes" name="apple-mobile-web-app-capable" />
	<meta content="black" name="apple-mobile-web-app-status-bar-style" />
	<meta content="telephone=no" name="format-detection" />
	<link type="text/css" rel="stylesheet" href="../../template/css/com/com.css"/>
	<link type="text/css" rel="stylesheet" href="../../template/css/list/style.css"/>

	<script src="../../template/js/jquery.js"></script>
	<script src="../../template/js/com/com.js"></script>
	<script src="../../template/js/com/template.js"></script>
	<script src="../../resource/my.js"></script>
	<script type="text/javascript" src="../../script/aes.js"></script>
	<script type="text/javascript" charset="UTF-8" src="../../script/naes.js"></script>
	<script type="text/javascript" src="../../script/modeecb.js"></script>
    <script type="text/javascript" src="../../script/tmd5.js"></script>
	<script type="text/javascript" src="../../script/constants.js"></script>
  <script src="../../script/api.js"></script>
          <link rel="stylesheet" href="../../resource/jinsuistyle.css" />
	<script type="text/javascript">
	    var alldm = "";
	    var allprice = "";
	    var hjprice = "";
	    var allsl = "";
	    var allspmc = "";
	    var allDiscount = "";
	    var allpprice = "";
	    var _openid = "";


	apiready=function(){
	 var YHDJ = $api.getStorage("YHDJ");
            if (YHDJ == null || YHDJ == '' || YHDJ == 'undefined' || typeof(YHDJ) == "undefined") {
          	$("#jstitle").text("职场生活(平台会员)");
            }else{
             $("#jstitle").text("职场生活("+YHDJ+")");
            }
	    if(api.systemType == "ios"){
		   document.getElementById("statusBar").style.background = "#298cb4";
		}
	    $api.fixStatusBar( $api.dom('header') );
		_openid = $api.getStorage('openid');
	    var header = $api.byId('jsheader');
	    if(api.systemType=='ios'){
			$api.fixIos7Bar(header);
		}
//		else{
//			$api.fixStatusBar(header);
//		}
	    api.addEventListener({
			    name: 'close'
			}, function(ret, err) {
			    if(ret.value.key==2){
			       api.closeWin();
			    }
			});
	    var html = "";
	    var ShoppingCart = utils.getParam("ShoppingCart");
	    if(ShoppingCart == "" || ShoppingCart == null){
	           html = "<label style=\"text-align:center;\">购物框中暂时没有商品哦，赶快去选购吧！</label>"
		       $("#pxui-form-info").html(html);
		       $("#allPrice").html("￥0");
		       $("#tjBtn").hide();
	    }else{
	      var jsonstr = JSON.parse(ShoppingCart);
	      var productlist = jsonstr.productlist;
		    if(productlist.length>0){
		       hjprice = jsonstr.totalAmount;
		       for(var i=0;i<productlist.length;i++){
		          if(i == productlist.length-1){
		             alldm += productlist[i].dm;
		             allprice += productlist[i].price;
		             allpprice += productlist[i].pprice;
		             allsl += productlist[i].sl;
		             allspmc += productlist[i].spmc;
		             allDiscount += productlist[i].discount;//parseFloat(productlist[i].discount)*parseInt(productlist[i].sl);
		          }else{
		             alldm += productlist[i].dm+",";
		             allprice += productlist[i].price+",";
		             allpprice += productlist[i].pprice+",";
		             allsl += productlist[i].sl+",";
		             allspmc += productlist[i].spmc+",";
		             allDiscount += productlist[i].discount+",";//parseFloat(productlist[i].discount)*parseInt(productlist[i].sl)+",";
		          }
		          html += "<p><table cellpadding=\"0\" border=\"0\" style=\"width:100%;\"><tr><td width=\"70%\">"+productlist[i].spmc+"</td>"
		                + "<td rowspan=\"2\" align=\"right\" valign=\"middle\"><a href=\"javascript:void(0)\" onclick=\"cart.deleteproduct('"+productlist[i].dm+"')\">"
		                + "<img width=\"15\" src=\"../../template/images/cart/del.png\" alt=\"移除\"/><label style=\"margin-bottom:8px;\">删除</label></a>"
		                + "</td></tr><tr><td>单价：￥"+productlist[i].price+"</td></tr><tr><td valign=\"top\"><font style=\"float:left;\">数量：</font>"
		                + "<img style=\"float:left;\" width=\"20\" onclick=\"updateNum('"+productlist[i].sl+"','"+productlist[i].dm+"',"+i+",'+')\" src=\"../../template/images/cart/jia.png\" alt=\"加\"/>"
		                + "<b class=\"spsl\" id=\"spsl"+i+"\">"+productlist[i].sl+"</b>"
		                + "<img style=\"float:left;\" width=\"20\" onclick=\"updateNum('"+productlist[i].sl+"','"+productlist[i].dm+"',"+i+",'-')\" src=\"../../template/images/cart/jian.png\" alt=\"减\"/>"
		                + "</td><td align=\"right\"><label>合计：￥"+(productlist[i].sl*productlist[i].price).toFixed(2)+"</label></td></tr></table>";
		       }
		       $("#pxui-form-info").html(html);
		       $("#allPrice").html("￥"+jsonstr.totalAmount);
		    }else{
		       html = "<p style=\"text-align:center;\"><label>购物框中暂时没有商品哦，赶快去选购吧！</label></p>"
		       $("#pxui-form-info").html(html);
		       $("#allPrice").html("￥0");
		       $("#tjBtn").hide();
		    }
	    }
	}

	 utils = {
		    setParam : function (name,value){
		        localStorage.setItem(name,value)
		    },
		    getParam : function(name){
		        return localStorage.getItem(name)
		    }
		}


	    product={
		    dm:0,
		    spmc:"",
		    sl:0,
		    price:0.00,
		};

			orderdetail={
			  //  username:"",
			  //  phone:"",
			  //  address:"",
			  //  zipcode:"",
			    totalNumber:0,
			    totalAmount:0.00
			}

			function updateNum(to,dm,i,sign){
			    var sl = parseInt($("#spsl" + i).html());
			    if(sign == "+"){
			      var num = sl+1;
			      cart.updateproductnum(dm,num);
			      window.location.reload();
			    }else{
			      if(sl>1){
			        var num = sl-1;
			        cart.updateproductnum(dm,num);
			        window.location.reload();
			      }
			    }
			}

			cart = {
			    //向购物车中添加商品
			    addproduct:function(product){
			        var ShoppingCart = utils.getParam("ShoppingCart");
			        if(ShoppingCart==null||ShoppingCart==""){
			                        //第一次加入商品
			            var jsonstr = {"productlist":[{"dm":product.dm,"spmc":product.spmc,"sl":product.sl,"price":product.price,"discount":product.discount,"pprice":product.pprice}],"totalNumber":product.sl,"totalAmount":(product.price*product.sl)};
			            utils.setParam("ShoppingCart",JSON.stringify(jsonstr));
			        }else{
			            var jsonstr = JSON.parse(ShoppingCart);
			            var productlist = jsonstr.productlist;
			            var result=false;
			            //查找购物车中是否有该商品
			            for(var i=0;i<productlist.length;i++){
			                if(productlist[i].dm==product.dm){
			                    productlist[i].sl=parseInt(productlist[i].sl)+parseInt(product.sl);
			                    result = true;
			                }
			            }
			            if(!result){
			                //没有该商品就直接加进去
			                productlist.push({"dm":product.dm,"spmc":product.spmc,"sl":product.sl,"price":product.price,"discount":product.discount,"pprice":product.pprice});
			            }
			                        //重新计算总价
			            jsonstr.totalNumber=parseInt(jsonstr.totalNumber)+parseInt(product.sl);
			//          jsonstr.totalAmount=parseFloat(jsonstr.totalAmount)+(parseInt(product.num)*parseFloat(product.price));
			            jsonstr.totalAmount=accAdd(jsonstr.totalAmount, product.sl*product.price);
			            orderdetail.totalNumber = jsonstr.totalNumber;
			            orderdetail.totalAmount = jsonstr.totalAmount;
			            //保存购物车
			            utils.setParam("ShoppingCart",JSON.stringify(jsonstr));
			        }
			    },

			    //修改给买商品数量
			    updateproductnum:function(dm,num){
			        var ShoppingCart = utils.getParam("ShoppingCart");
			        var jsonstr = JSON.parse(ShoppingCart);
			        var productlist = jsonstr.productlist;
			        for(var i=0;i<productlist.length;i++){
			            if(productlist[i].dm==dm){
			                jsonstr.totalNumber=parseInt(jsonstr.totalNumber)+(parseInt(num)-parseInt(productlist[i].sl));
			//              jsonstr.totalAmount=parseFloat(jsonstr.totalAmount)+((parseInt(num)*parseFloat(productlist[i].price))-parseInt(productlist[i].num)*parseFloat(productlist[i].price));
			                jsonstr.totalAmount=accAdd(jsonstr.totalAmount, accSub(num*productlist[i].price, productlist[i].sl*productlist[i].price));
			                productlist[i].sl=parseInt(num);
			                orderdetail.totalNumber = jsonstr.totalNumber;
			                orderdetail.totalAmount = jsonstr.totalAmount;
			                utils.setParam("ShoppingCart",JSON.stringify(jsonstr));
			                return;
			            }
			        }
			    },

			    //获取购物车中的所有商品
			    getproductlist:function(){
			        var ShoppingCart = utils.getParam("ShoppingCart");
			        if (!ShoppingCart) {
			                return null;
			        }
			        var jsonstr = JSON.parse(ShoppingCart);
			        var productlist = jsonstr.productlist;
			        orderdetail.totalNumber = jsonstr.totalNumber;
			        orderdetail.totalAmount = jsonstr.totalAmount;
			        return productlist;
			    },

			    //获取整个购物车
			    getshopcart:function(){
			            var ShoppingCart = utils.getParam("ShoppingCart");
			            return ShoppingCart;
			    },

			    //判断购物车中是否存在商品
			    existproduct:function(id){
			        var ShoppingCart = utils.getParam("ShoppingCart");
			        var jsonstr = JSON.parse(ShoppingCart);
			        var productlist = jsonstr.productlist;
			        var result=false;
			        for(var i=0;i<productlist.length;i++){
			            if(productlist[i].dm==dm){
			                result = true;
			            }
			        }
			        return result;
			    },

			    // 通过id获取商品
			    getproductbyid:function(id) {
			            var ShoppingCart = utils.getParam("ShoppingCart");
			        var jsonstr = JSON.parse(ShoppingCart);
			        var productlist = jsonstr.productlist;
			        var result=false;
			        for(var i=0;i<productlist.length;i++){
			            if(productlist[i].dm==dm){
			                return productlist[i];
			            }
			        }
			        return null;
			    },
			    //删除购物车中商品
			    deleteproduct:function(id){
            api.actionSheet({
                title: '是否删除商品',
                cancelTitle: '取消',
                buttons: ['删除']
            }, function(ret, err){
              if(ret.buttonIndex==1){
                var ShoppingCart = utils.getParam("ShoppingCart");
  			        var jsonstr = JSON.parse(ShoppingCart);
  			        var productlist = jsonstr.productlist;
  			        var list=[];
  			        for(var i=0;i<productlist.length;i++){
  			            if(productlist[i].dm==id){
  			                jsonstr.totalNumber=parseInt(jsonstr.totalNumber)-parseInt(productlist[i].sl);
  			//              jsonstr.totalAmount=parseFloat(jsonstr.totalAmount)-parseInt(productlist[i].num)*parseFloat(productlist[i].price);
  			                jsonstr.totalAmount=accSub(jsonstr.totalAmount, productlist[i].sl*productlist[i].price);
  			            }else{
  			                list.push(productlist[i]);
  			            }
  			        }
  			        jsonstr.productlist = list;
  			        orderdetail.totalNumber = jsonstr.totalNumber;
  			        orderdetail.totalAmount = jsonstr.totalAmount;
  			        utils.setParam("ShoppingCart",JSON.stringify(jsonstr));
  			        window.location.reload();
              }
            });
			    },
			    //清空购物车
			    clear:function(){
            api.actionSheet({
              title: '是否清空购物车',
              cancelTitle: '取消',
              buttons: ['清空']
            }, function(ret, err){
                if( ret ){
                     if(ret.buttonIndex==1){
                       utils.setParam("ShoppingCart", "");
                       window.location.reload();
                     }
                }else{
                     alert( JSON.stringify( err ) );
                }
            });
			    }
			};


            function add(a, b) {
			    var c, d, e;
			    try {
			        c = a.toString().split(".")[1].length;
			    } catch (f) {
			        c = 0;
			    }
			    try {
			        d = b.toString().split(".")[1].length;
			    } catch (f) {
			        d = 0;
			    }
			    return e = Math.pow(10, Math.max(c, d)), (mul(a, e) + mul(b, e)) / e;
			}
			 
			// 浮点数相减
			function sub(a, b) {
			    var c, d, e;
			    try {
			        c = a.toString().split(".")[1].length;
			    } catch (f) {
			        c = 0;
			    }
			    try {
			        d = b.toString().split(".")[1].length;
			    } catch (f) {
			        d = 0;
			    }
			    return e = Math.pow(10, Math.max(c, d)), (mul(a, e) - mul(b, e)) / e;
			}
			 
			// 浮点数相乘
			function mul(a, b) {
			    var c = 0,
			        d = a.toString(),
			        e = b.toString();
			    try {
			        c += d.split(".")[1].length;
			    } catch (f) {}
			    try {
			        c += e.split(".")[1].length;
			    } catch (f) {}
			    return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
			}
			 
			// 浮点数相除
			function div(a, b) {
			    var c, d, e = 0,
			        f = 0;
			    try {
			        e = a.toString().split(".")[1].length;
			    } catch (g) {}
			    try {
			        f = b.toString().split(".")[1].length;
			    } catch (g) {}
			    return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), mul(c / d, Math.pow(10, f - e));
			}




			// 两个浮点数求和
			function accAdd(num1, num2) {
			        var r1, r2, m;
			        try {
			                r1 = num1.toString().split('.')[1].length;
			        } catch(e) {
			                r1 = 0;
			        }
			        try {
			                r2 = num2.toString().split(".")[1].length;
			        } catch(e) {
			                r2 = 0;
			        }
			        m = Math.pow(10, Math.max(r1, r2));
			        // return (num1*m+num2*m)/m;
			        return Math.round(num1 * m + num2 * m) / m;
			}

			// 两个浮点数相减
			function accSub(num1, num2) {
			        var r1, r2, m;
			        try {
			                r1 = num1.toString().split('.')[1].length;
			        } catch(e) {
			                r1 = 0;
			        }
			        try {
			                r2 = num2.toString().split(".")[1].length;
			        } catch(e) {
			                r2 = 0;
			        }
			        m = Math.pow(10, Math.max(r1, r2));
			        n = (r1 >= r2) ? r1 : r2;
			        return (Math.round(num1 * m - num2 * m) / m).toFixed(n);
			}

			// 两数相除
			function accDiv(num1, num2) {
			        var t1, t2, r1, r2;
			        try {
			                t1 = num1.toString().split('.')[1].length;
			        } catch(e) {
			                t1 = 0;
			        }
			        try {
			                t2 = num2.toString().split(".")[1].length;
			        } catch(e) {
			                t2 = 0;
			        }
			        r1 = Number(num1.toString().replace(".", ""));
			        r2 = Number(num2.toString().replace(".", ""));
			        return (r1 / r2) * Math.pow(10, t2 - t1);
			}

			// 浮点数相乘
			function accMul(num1, num2) {
			        var m = 0, s1 = num1.toString(), s2 = num2.toString();
			        try {
			                m += s1.split(".")[1].length
			        } catch(e) {
			        };
			        try {   
			                m += s2.split(".")[1].length
			        } catch(e) {
			        };
			        return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
			}


			   	function tjqd(){
			   	 var jp = $api.getStorage("dl_phone");
		if(jp==null||jp==''||typeof(jp)==undefined||jp=='undefined'){
		api.alert({
			msg:'请登录'
        },function(ret,err){
        	api.openWin({
	            name: 'login',
	            url: '../login.html'
	            //url:'html/jskp/jskp.html'
            });
        });
	}else{
   		api.toast({
	           msg:'数据提交中...'
           });
        var callback = function(ret, err){
            if (ret) {
                 console.log(JSON.stringify(ret));
                 var data = eval("("+ ret.json +")");
                 api.openWin({
				    name: 'address',
				    url: './address.html',
				    pageParam: {
				        orderid: data.orderid,
				        allPrice: hjprice,
				        allspmc: allspmc
				    }
				    //slidBackEnabled:false
				});
		    } else {
		        api.alert("提交失败！");
		    }
        }
        var content = {"spdm":alldm,"hj":hjprice.toString(),"price":allprice.toString(),"sl":allsl,"discount":allDiscount,"pprice":allpprice,"userid":_openid};
        apiAjax(JSON.stringify(content),url.zctjurl,callback);
        }
   	}
   	
   		function closeWin(){
         api.closeWin();
        }

      function goMain(){
        api.closeWin();
      }
	</script>

   	<style type="text/css">
   		#pxui-form-info .delsp {
   			right:5px;
   			width:30px;
   			height:30px;
   		}
   		.pxui-form-info p{
   			padding:5px;
   		}
   		#pxui-form-info td{font-size:14px;}
   		#pxui-form-info td .spsl{float:left;width:30px;margin:0 5px;text-align:center;background-color:#FFFFFF;border:1px solid #CDCDCD;}
   	</style>
  </head>
  <body style="background-color: #ffffff">
   <header id="statusBar">
    <div id="jsheader">
        <nav id="jsnav">
            <a id="jsleft" tapmode="" onclick="api.closeToWin({name: 'root'});">
                <span>主页</span>
            </a>
            <div id="jsmiddle">
                <strong id="jstitle">职场生活</strong>
            </div>
            <a id="jsright">
              <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
            </a>
        </nav>
    </div>
    </header>
    <div class="com-content style-page">
		<!-- <div class="com-header-area" id="js-com-header-area">
			<a href="" class="com-header-logo"
				style="width:100%;color:#FFFFFF;text-align:center;font-size:24px;">职场生活</a>
			<div class="clear"></div>
		</div> -->
		<div class="com-header-area" id="js-com-header-area">
			<!-- <a href="" class="com-header-logo"
				style="width:100%;color:#FFFFFF;text-align:center;font-size:20px;">职场生活</a> -->
				<img src="../../image/tb.png" style="width:100%;"/>
				<ul class="menu_ul">
					<a href="main.html" class="selected"><li>首  页</li></a>
					<a href="history.html"><li>历 史 订 单</li></a>
					<a href="bill.html"><li>购 物 筐</li></a>
				</ul>
			<dfn></dfn>
			<div class="clear"></div>
		</div>  
		
		<div class="com-content-area" id="js-com-content-area">
			<!-- <div class="pxui-tab pxui-tab-nav pxui-tab-no-top">
				<a href="main.html"><i></i>首 页<span></span></a>
				<a href="history.html"><i></i>历史订单<span></span></a>
				<a href="bill.html" class="selected"><i></i>购物筐<span></span></a>
			</div> -->
			
			<div class="page-role cart-page cart-index-page">
				<div class="page-title" style="margin:0px 5px 10px 5px;">
					<a href="#" onclick="closeWin();" class="return">返 回</a>购物筐
				</div>
				<form id="js-form" action="/order/trolly" method="post">
					<div class="pxui-area goodlist">
						<div id="pxui-form-info" class="pxui-form-info" style="padding-top:2px;min-height: 45%;">
								<!--div class="img80"><dfn></dfn><img src="http://ypt.blob.core.chinacloudapi.cn/picture/fyz/jrcqsmjz.jpg"/></div-->


						</div>

						<div style="padding:0 10px;display:none;">
							物流选择：<select style="width:61%;margin-top:10px;padding:5px;" id="wlfs">
									<option value="1" selected="selected">快递</option>
								</select>
						</div>
						<div style="padding:0 10px;display:none;">
							送至地区：<select style="width:30%;margin-top:10px;padding:5px;" id="address" onchange="dqdm()">
								<option value="5101">成都市</option>
								<option value="5102">自贡市</option>
								<option value="5103">攀枝花市</option>
								<option value="5104">泸州市</option>
								<option value="5105">德阳市</option>
								<option value="5106">绵阳市</option>
								<option value="5107">广元市</option>
								<option value="5108">遂宁市</option>
								<option value="5109">内江市</option>
								<option value="5110">资阳市</option>
								<option value="5111">乐山市</option>
								<option value="5112">眉山市</option>
								<option value="5113">南充市</option>
								<option value="5114">宜宾市</option>
								<option value="5115">广安市</option>
								<option value="5116">达州市</option>
								<option value="5117">巴中市</option>
								<option value="5118">雅安市</option>
								<option value="5119">阿坝州</option>
								<option value="5120">甘孜州</option>
								<option value="5121">凉山州</option>
							</select>
							<select style="width:30%;margin-top:10px;padding:5px;" id="address_qx" onchange="fare()">

							</select>
							<br/>
							<label style="float:left;line-height:40px;" >运费：<b class="pxui-color-red" id="yf">￥0</b></label>
						</div>
						<div id="tjBtn" style="font-weight:bold;width:98%;float:left;text-align:right;">
							<input type="button" class="pxui-light-button addtocart" style="float:left;margin-left:10px;" value="置空清单" onclick="cart.clear();" />
							总计：<label class="pxui-color-red" id="allPrice">￥</label>
							<input type="button" class="pxui-light-button addtocart" id="mytj" value="提交清单" onclick="tjqd()" />
						</div>

					</div>
				</form>
			</div>
		</div>


		<!-- 公共底部，必须引用 -->
<!-- 		<div class="com-footer-area" id="js-com-footer-area">
			<div class="com-footer-nav">
		<a href="main.html">首 页</a>
		<a href="history.html">历史订单</a>
		<a href="bill.html">购物筐</a>
			</div>
			<div class="com-footer">
		 © 2012-2018 四川航天金穗高技术有限公司 <br /> All Rights Reserved
		联系电话：028-66778822
			</div>
		</div> -->
		<div class="com-footer-area footer" id="js-com-footer" style="width:100%;">
				<img src="../../image/db.png" style="width:100%;"/>
			</div>
	</div>
  </body>
</html>
